﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.BusinessLogicLayer;
using CuaHangMayTinh.Utilities;

namespace CuaHangMayTinh.PresentationLayer
{
    /// <summary>
    /// form hiển thị thông tin nhân viên, thêm, xóa, sửa, tìm kiếm nhân viên
    /// </summary>
    public partial class FormNhanVien : Form,IFormTV<NhanVienEntity>
    {
        /// <summary>
        /// biến lưu text lối của form
        /// </summary>
        string Text = string.Empty;

        private DataTable nghiepVuEntity = NghiepVuManager.Find_All();

        enum Trangthai
        {
            Đang_làm_việc,
            Đang_xin_nghỉ,
            Nghỉ_việc
        }

        /// <summary>
        /// tables chua danh sach entity
        /// </summary>
        public DataTable Table { get; set; }
        /// <summary>
        /// danh sách nhân viên
        /// </summary>
        private List<NhanVienEntity> nhavieneEntities; 

        /// <summary>
        /// hàm khởi tạo
        /// </summary>
        public FormNhanVien()
        {
            InitializeComponent();
            nhavieneEntities=new List<NhanVienEntity>();
            LoadListView();
        }

        public FormNhanVien(bool[] so)
            : this()
        {
            if (so[0])
            {
                toolStripBtn_TimKiem.Enabled = true;
            }
            if (so[1])
            {
                toolStripBtn_Them.Enabled = true;
                themToolStripMenuItem.Enabled = true;
            }
            if (so[2])
            {
                toolStripBtn_Xoa.Enabled = true;
                xoaToolStripMenuItem.Enabled = true;
            }
            if (so[3])
            {
                toolStripBtn_Sua.Enabled = true;
                suaToolStripMenuItem.Enabled = true;
            }
        }

        /// <summary>
        /// load thông tin từ datable lên datagirview 
        /// </summary>
        /// <param name="dt">datatable cần load lên</param>
        public void LoadListView(DataTable dt)
        {
            dataGridViewX_Select.DataSource = dt;
            if (dataGridViewX_Select.Columns.Count > 0)
            {
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        /// <summary>
        /// thêm click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_them_click(object sender, EventArgs e)
        {
            var nhanVienThem = new FormNhanVienThem();
            if (nhanVienThem.ShowDialog() == DialogResult.OK)
            {
                //goi them nhan vien
                LoadListView();
            }
        }

        /// <summary>
        /// lấy danh sách nhân viên lên datagridview
        /// </summary>
        public void LoadListView()
        {
            Table = DataUtil.ChangeColumn(NhanVienManager.Find_All(true));
            if (Table != null)
            {
                dataGridViewX_Select.DataSource = Table;
                dataGridViewX_Select.DataSource = Table;
                Table.Columns.Add("T Trạng", typeof(string));
                var t = Table.AsEnumerable().Where(x => x.Field<int>("Tình trạng") == 0);
                foreach (var dataRow in t)
                {
                    dataRow["T Trạng"] = "Đang làm việc";
                }
                var t1 = Table.AsEnumerable().Where(x => x.Field<int>("Tình trạng") == 1);
                foreach (var dataRow in t1)
                {
                    dataRow["T Trạng"] = "Đang xin nghỉ";
                }
                var t2 = Table.AsEnumerable().Where(x => x.Field<int>("Tình trạng") == 2);
                foreach (var dataRow in t2)
                {
                    dataRow["T Trạng"] = "Đã nghỉ việc";
                }
                dataGridViewX_Select.Columns["Tình trạng"].Visible = false;
                dataGridViewX_Select.Columns[0].Visible = false;
            }
        }

        /// <summary>
        /// nút xóa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_xoa_click(object sender, EventArgs e)
        {
            if (dataGridViewX_Select.SelectedRows.Count > 0)
            {
                int i = 0;
                var c = MessageBox.Show("Bạn muốn xóa hay không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (c == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dataGridViewX_Select.SelectedRows)
                    {
                        if (NhanVienManager.Delete((int)row.Cells[0].Value,ref Text))
                        {
                            i++;
                        }
                        else
                        {
                            MessageBox.Show(Text);
                        }
                    }
                    MessageBox.Show("Xóa thành công " + i + " nhân viên!");
                    LoadListView();
                }
            }
        }

        /// <summary>
        /// nút sửa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_sua_click(object sender, EventArgs e)
        {
            if (dataGridViewX_Select.SelectedRows.Count > 0)
            {
                var row = dataGridViewX_Select.SelectedRows[0];
                var entity = new NhanVienEntity()
                {
                    MaNV = (int)row.Cells[0].Value,
                    TenNV = (string)row.Cells[1].Value,
                    NghiepVu = SubGroupUserManager.ConvertToList(SubGroupUserManager.Find_Ten((string)row.Cells[2].Value))[0].IDSubGroupUser,
                    CMND = (string)row.Cells[3].Value,
                    DiaChi = (string)row.Cells[4].Value,
                    SoDT = (string)row.Cells[5].Value,
                    Email = (string)row.Cells[6].Value,
                    NgaySinh = (DateTime)row.Cells[7].Value,
                    GioiTinh = (bool)row.Cells[8].Value,
                    TinhTrang = (int)row.Cells[9].Value,
                    GhiChu = (string)row.Cells[10].Value
                };
                FormNhanVienThem formNhanVienThem = new FormNhanVienThem(entity);
                if (formNhanVienThem.ShowDialog() == DialogResult.OK)
                    LoadListView();
            }
        }

        /// <summary>
        /// nút tìm kiếm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_timkiem_click(object sender, EventArgs e)
        {
            FormNhanVienTimKiem formNhanVienTimKiem = new FormNhanVienTimKiem(Table);
            formNhanVienTimKiem.ShowDialog();
        }
        
        /// <summary>
        /// load datatable lên datagirview bằng phương thức finall 
        /// </summary>
        /// 

        /// <summary>
        /// bắt sự kiện khi mà thay đổi text của text box của thanh tìm kiếm nhanh
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStriptb_timnhanh_TextChanged(object sender, EventArgs e)
        {
            string text = (sender as ToolStripTextBox).Text;
            if (text != "")
            {
                DataTable dt = Table;
                var table2 =dt.AsEnumerable().Where(x =>DataUtil.convertToUnSign3(x.Field<string>("Tên nhân viên").ToLower())
                                    .StartsWith(DataUtil.convertToUnSign3(text.ToLower())));
                if (table2.Any())
                {
                    LoadListView(table2.CopyToDataTable());
                }
                else
                {
                    LoadListView(null);
                }
            }
            else
            {
                LoadListView(Table);
            }
        }

        /// <summary>
        /// thêm của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void themToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_them_click(sender, e);
        }

        /// <summary>
        /// xóa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_xoa_click(sender, e);
        }

        /// <summary>
        /// sửa của menustrip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void suaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btn_sua_click(sender, e);
        }

        private void FormNhanVien_Load(object sender, EventArgs e)
        {

        }
    }
}
